import time

from django.db.models import Q

# Create your views here.
from django.views import View
from rest_framework.views import APIView
from django.http import JsonResponse
import numpy as np
from .models import NavInfo,FictionInfo,ZhangjieInfo,TextInfo
from .serializers import Navserializer,Fictionserializer, TextSerialziers
# Create your views here.
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.mixins import ListModelMixin,RetrieveModelMixin
from rest_framework.viewsets import GenericViewSet

from searchs.searwork import start

class NavView(ModelViewSet):
    serializer_class = Navserializer
    queryset = NavInfo.objects.all()



class FictionView(APIView):
    def get(self,request,pk):
        data = FictionInfo.objects.filter(navid_id=pk)[0:20]
        # for i in data:
        #     zhang = ZhangjieInfo.objects.filter(fiction_id=i.id)[1]


        ser = Fictionserializer(data,many=True,context={'request': request})
        return Response(ser.data)
class FicView(GenericViewSet,RetrieveModelMixin,ListModelMixin):
    '''
    获取小说
    '''
    queryset = FictionInfo.objects.all()
    serializer_class = Fictionserializer

class ZhangjieView(View):
    '''
    章节
    '''
    def get(self,request,pk):
        list1=[]
        da = ZhangjieInfo.objects.filter(fiction_id=pk)


        for i in da:
            data={
                'id':i.id,
                'title':i.title
            }

            list1.append(data)
        return JsonResponse(list1,safe=False)

class Detailtext(GenericViewSet,RetrieveModelMixin):
    '''
    先说文本视图
    '''
    serializer_class = TextSerialziers
    queryset = TextInfo.objects.all()

class NewistopView(View):
    '''
    首页推荐
    '''
    def get(self,request):
        list1=[]
        new = FictionInfo.objects.all().order_by('-id')[:20]
        for i in new:
            aothors = i.author[7:]
            types = i.navid.name[:2]

            data={
                'id':i.id,
                'title':i.fiction_title,
                'aothor':aothors,
                'type_nav':types,

            }
            list1.append(data)
        np.random.shuffle(list1)#让取出来的列表随机
        return JsonResponse(list1,safe=False)

class allficView(APIView):
    '''获取全部小说'''
    def get(self,request):
        data = FictionInfo.objects.all()
        ser = Fictionserializer(data,many=True,context={'request': request})
        popo = np.random.shuffle(ser.data)  # 随机洗牌展示
        return Response(ser.data,status=200)


class SearchView(APIView):
    '''
    搜索的接口
    '''
    def get(self,request,search):
        print(search)
        datas = FictionInfo.objects.filter(Q(fiction_title__contains=search)|Q(author__contains=search))
        if datas:
            ser = Fictionserializer(datas,many=True,context={'request': request})
            print(ser.data)
            return Response(ser.data,status=200)
        else:
            start.delay(search)
            time.sleep(6)
            datas = FictionInfo.objects.filter(Q(fiction_title__contains=search) | Q(author__contains=search))
            ser = Fictionserializer(datas,many=True,context={'request': request})
            return  Response(ser.data,status=200)


